/**
 * TmCustomAlertServiceImpl.java
 * Created at 2017-10-30
 * Created by zhanghailin
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.system.dao.ITmCustomAlertDao;
import com.broadtext.rms.system.model.TmCustomAlert;
import com.broadtext.rms.system.service.ITmCustomAlertService;

/**
 * <p>ClassName: TmCustomAlertServiceImpl</p>
 * <p>Description: 自定义告警服务层</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2017-10-30</p>
 */
@Service("tmCustomAlertService")
public class TmCustomAlertServiceImpl extends EntityServiceImpl<TmCustomAlert> implements ITmCustomAlertService{
    
    
    /**
     * 注入电池监控Dao
     */
    @Autowired
    @Qualifier("tmCustomAlertDao")
    private ITmCustomAlertDao tmCustomAlertDao;
    
    /**
     * 
     * <p>
     * Description: 注解入相关DAO
     * </p>
     * 
     * @param entityDao
     */
    @Autowired
    @Qualifier("tmCustomAlertDao")
    public void setEntityDao(EntityDao<TmCustomAlert> entityDao) {
        super.entityDao = entityDao;
    }

    /** 保存自定义告警
     * <p>Title: saveCustomAlert</p>
     * <p>Description: </p>
     * @param t
     * @return
     * @see com.broadtext.rms.system.service.ITmCustomAlertService#saveCustomAlert(com.broadtext.rms.system.model.TmCustomAlert)
     */
    @Override
    public Boolean saveCustomAlert(TmCustomAlert t) throws ServiceException{
        Boolean flag = true;
        Boolean flag2 = true;
        try {
            String alertDefinitionId = UUIDUtils.getUUID();
            t.setAlertDefinitionId(alertDefinitionId);
            //空字符串转null,防止数据库float字段报错
            if(StringUtils.isBlank(t.getComparisonValueOne())){
                t.setComparisonValueOne(null);
            }
            if(StringUtils.isBlank(t.getComparisonValueTwo())){
                t.setComparisonValueTwo(null);
            }
            flag = this.entityDao.insertRecord("com.broadtext.rms.system.model.TmCustomAlert.saveCustomAlert", t);
            flag2 = this.entityDao.insertRecord("com.broadtext.rms.system.model.TmCustomAlert.saveCustomAlertDefinition", t);
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return flag&&flag2;
    }
    
    /** 修改自定义告警
     * <p>Title: saveCustomAlert</p>
     * <p>Description: </p>
     * @param t
     * @return
     * @see com.broadtext.rms.system.service.ITmCustomAlertService#saveCustomAlert(com.broadtext.rms.system.model.TmCustomAlert)
     */
    @Override
    public Boolean updateCustomAlert(TmCustomAlert t) throws ServiceException{
        Boolean flag = true;
        Boolean flag2 = true;
        try {
            //String alertDefinitionId = UUIDUtils.getUUID();
            //t.setAlertDefinitionId(alertDefinitionId);
            if(StringUtils.isBlank(t.getComparisonValueOne())){
                t.setComparisonValueOne(null);
            }
            if(StringUtils.isBlank(t.getComparisonValueTwo())){
                t.setComparisonValueTwo(null);
            }
            flag = this.entityDao.updateRecord("com.broadtext.rms.system.model.TmCustomAlert.updateCustomAlert", t);
            flag2 = this.entityDao.updateRecord("com.broadtext.rms.system.model.TmCustomAlert.updateCustomAlertDefinition", t);
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return flag&&flag2;
    }
    
    /**
     * 
     * <p>Description: 批量删除 </p>
     * @param idArray 主键数组
     * @param currentUser 当前登录用户名
     * @throws DaoException 异常
     */
    public void delete(String[] idArray, String currentUser) throws ServiceException {
        try{
          //循环删除 
            for (String id : idArray) {
                TmCustomAlert t = new TmCustomAlert();
                t.setPkId(id);
                t.setUpdateDate(new Date());
                t.setUpdateUser(currentUser);
                t.setDelFlag("1");
                this.entityDao.updateRecord("com.broadtext.rms.system.model.TmCustomAlert.deleteRecord", t);
            }
        }catch(DaoException e){
            e.printStackTrace();
            throw new ServiceException(e);
        }
        
    }

    /**(non-Javadoc)
     * <p>Title: getTmCustomAlertList</p>
     * <p>Description: </p>
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.ITmCustomAlertService#getTmCustomAlertList()
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<TmCustomAlert> getTmCustomAlertList() throws ServiceException {
        List<TmCustomAlert> list = new ArrayList<TmCustomAlert>();
        try {
            list = this.entityDao.findByProperty("com.broadtext.rms.system.model.TmCustomAlert.getTmCustomAlertList", null);
        } catch (DaoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return list;
    }

    /** (non-Javadoc)
     * <p>Title: checkRepeatCode</p>
     * <p>Description: </p>
     * @param alertCode
     * @param pkId
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.ITmCustomAlertService#checkRepeatCode(java.lang.String, java.lang.String)
     */
    @Override
    public Boolean checkRepeatCode(String alertCode, String pkId) throws ServiceException {
        Boolean flag = true;
        Map<String, Object> queryMap = new HashMap<String, Object>();
        if(StringUtils.isNotBlank(alertCode)){
            queryMap.put("alertCode", alertCode);
        }
        if(StringUtils.isNotBlank(pkId)){
            queryMap.put("pkId", pkId);
        }
        long count = 0 ;
        try {
            count = this.tmCustomAlertDao.checkRepeatCode(queryMap);
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        if(count!=0){
            flag = false;
        }
        return flag;
    }
    
    

}
